home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / PARSESTR.H < prev    next >
C/C++ Source or Header  |  1992-03-29  |  5KB  |  133 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef PARSESTREAM_H
  19. #define PARSESTREAM_H
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23. #include "streambuf.h"
  24.  
  25. // A parsebuf is a streambuf optimized for scanning text files.
  26. // It keeps track of line and column numbers.
  27. // It is guaranteed to remember the entire current line,
  28. // as well the '\n'-s on either side of it (if they exist).
  29. // You can arbitrarily seek (or unget) within this extended line.
  30. // Other backward seeks are not supported.
  31. // Normal read semantics are supported (and hence istream operators like >>).
  32.  
  33. class parsebuf : public streambuf {
  34.   protected:
  35.     // Buffer = [start1..end1]++[start2..end2].
  36.     fpos_t pos_at_line_start;
  37.     long _line_length;
  38.     unsigned long __line_number;
  39.     char *buf_start;
  40.     char *buf_end;
  41.     unsigned int current_in_2nd_buffer : 1;
  42.  
  43.   public:
  44.     parsebuf *chain;
  45.  
  46.     // Return column number (raw - don't handle tabs etc).
  47.     // Retult can be -1, meaning: at '\n' before current line.
  48.     virtual int tell_in_line();
  49.  
  50.     // seek to (raw) column I in current line.
  51.     // Result is new (raw) column position - differs from I if unable to seek.
  52.     // Seek to -1 tries to seek to before previous LF.
  53.     virtual int seek_in_line(int i);
  54.  
  55.     // Note: there is no "current line" initially, until something is read.
  56.  
  57.     // Current line number, starting with 0.
  58.     // If tell_in_line()==-1, then line number of next line.
  59.     int line_number() { return __line_number; }
  60.  
  61.     // Length of current line, not counting either '\n'.
  62.     int line_length() { return _line_length; }
  63.     // Current line - not a copy, so file ops may trash it. 
  64.     virtual char* current_line();
  65.     streampos seekoff(streamoff, _seek_dir, int);
  66.     virtual streambuf* setbuf(char* p, int len);
  67.   protected:
  68.     parsebuf() : streambuf() { chain= NULL;
  69.     __line_number = 0; pos_at_line_start = 0; _line_length = -1; }
  70.     virtual int pbackfail(int c);
  71. };
  72.  
  73. // A string_parsebuf is a parsebuf whose source is a fixed string.
  74.  
  75. class string_parsebuf : public parsebuf {
  76.   public:
  77.     int do_delete;
  78.     string_parsebuf(char *str, int len, int delete_at_close=0);
  79.     virtual int underflow();
  80.     virtual char* current_line();
  81.     virtual int seek_in_line(int i);
  82.     virtual int tell_in_line();
  83.     char *left() const { return base(); }
  84.     char *right() const { return ebuf(); }
  85. //    streampos seekoff(streamoff, _seek_dir, int);
  86. };
  87.  
  88. // A func_parsebuf calls a given function to get new input.
  89. // Each call returns an entire NUL-terminated line (without the '\n').
  90. // That line has been allocated with malloc(), not new.
  91. // The interface is tailored to the GNU readline library.
  92. // Example:
  93. // char* DoReadLine(void* arg)
  94. // {
  95. //   char *line = readline((char*)arg); /* 'arg' is used as prompt. */
  96. //   if line == NULL) { putc('\n', stderr); return NULL; }
  97. //   if (line[0] != '\0') add_history(line);
  98. //    return line;
  99. // }
  100. // char PromptBuffer[100] = "> ";
  101. // func_parsebuf my_stream(DoReadLine, PromptBuffer);
  102.  
  103. typedef char *(*CharReader)(void *arg);
  104. class istream;
  105.  
  106. class func_parsebuf : public parsebuf {
  107.   public:
  108.     void *arg;
  109.     CharReader read_func;
  110.     int backed_up_to_newline;
  111.     func_parsebuf(CharReader func, void *argm = NULL);
  112.     int underflow();
  113.     virtual int tell_in_line();
  114.     virtual int seek_in_line(int i);
  115.     virtual char* current_line();
  116. };
  117.  
  118. // A general_parsebuf is a parsebuf which gets its input from some
  119. // other streambuf. It explicitly buffers up an entire line.
  120.  
  121. class general_parsebuf : public parsebuf {
  122.   public:
  123.     streambuf *sbuf;
  124.     int delete_buf; // Delete sbuf when destroying this.
  125.     general_parsebuf(streambuf *buf, int delete_arg_buf = 0);
  126.     int underflow();
  127.     virtual int tell_in_line();
  128.     virtual int seek_in_line(int i);
  129.     ~general_parsebuf();
  130.     virtual char* current_line();
  131. };
  132. #endif /*!defined(PARSESTREAM_H)*/
  133.